local hx__neifa = fk.CreateSkill {
  name = "hx__neifa",
}


hx__neifa:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and player:hasSkill(hx__neifa.name) and player.phase == Player.Play
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      player:drawCards(2, hx__neifa.name)
      local choices = room:askToChoices(player, {
        choices = {"@hx__neifa__basic", "@hx__neifa__trick", "@hx__neifa__equip"},
        min_num = 1,
        max_num = 2,
        prompt = "#hx__neifa-choice",
        can_cancel = true,
        skill_name = hx__neifa.name,
      })
      if #choices == 0 then return false end
      for _, choice in ipairs(choices) do
        if choice == "@hx__neifa__basic" then
          room:addPlayerMark(player, "@hx__neifa__basic", 1)
        elseif choice == "@hx__neifa__trick" then
          room:addPlayerMark(player, "@hx__neifa__trick", 1)
        elseif choice == "@hx__neifa__equip" then
          room:addPlayerMark(player, "@hx__neifa__equip", 1)
        end
      end
      local discardCount = 0
      for _, cardId in ipairs(player:getCardIds("h")) do
        local card = Fk:getCardById(cardId)
        if table.contains(choices, "@hx__neifa__basic") and card.type == Card.TypeBasic then
          room:throwCard({cardId}, hx__neifa.name, player, player)
          discardCount = discardCount + 1
        elseif table.contains(choices, "@hx__neifa__trick") and card.type == Card.TypeTrick then
          room:throwCard({cardId}, hx__neifa.name, player, player)
          discardCount = discardCount + 1
        elseif table.contains(choices, "@hx__neifa__equip") and card.type == Card.TypeEquip then
          room:throwCard({cardId}, hx__neifa.name, player, player)
          discardCount = discardCount + 1
        end
      end
      if discardCount > 0 and discardCount < 8 then
        room:setPlayerMark(player, "@hx__neifa__draw", discardCount)
      elseif discardCount >= 8 then
        room:setPlayerMark(player, "@hx__neifa__draw", 8)
      end
    end
  end,
})

hx__neifa:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardUsing then
      return target == player and player:hasSkill(hx__neifa.name) and player:getMark("@hx__neifa__draw") > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.CardUsing and player:getMark("@hx__neifa__draw") > 0 then
      local card = data.card
      local basic_mark = player:getMark("@hx__neifa__basic")
      local trick_mark = player:getMark("@hx__neifa__trick")
      local equip_mark = player:getMark("@hx__neifa__equip")
      local drawCount = player:getMark("@hx__neifa__draw")
      if basic_mark > 0 then
        if trick_mark > 0 and card.type == Card.TypeEquip then
          player:drawCards(drawCount, hx__neifa.name)
        end
        if equip_mark > 0 and card.type == Card.TypeTrick then
          player:drawCards(drawCount, hx__neifa.name)
        end
        if trick_mark == 0 and equip_mark == 0 and card.type ~= Card.TypeBasic then
          player:drawCards(drawCount, hx__neifa.name)
        end
      elseif trick_mark > 0 then
        if equip_mark > 0 and card.type == Card.TypeBasic then
          player:drawCards(drawCount, hx__neifa.name)
        end
        if basic_mark == 0 and equip_mark == 0 and card.type ~= Card.TypeTrick then
          player:drawCards(drawCount, hx__neifa.name)
        end
      elseif equip_mark > 0 then
        if basic_mark == 0 and trick_mark == 0 and card.type ~= Card.TypeEquip then
          player:drawCards(drawCount, hx__neifa.name)
        end
      end
    end
  end,
})
hx__neifa:addEffect(fk.EventPhaseEnd, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__neifa.name) and player.phase == Player.Play
    and (player:getMark("@hx__neifa__draw") > 0 or player:getMark("@hx__neifa__basic") > 0 or player:getMark("@hx__neifa__trick") > 0 or player:getMark("@hx__neifa__equip") > 0)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@hx__neifa__draw", 0)
    room:setPlayerMark(player, "@hx__neifa__basic", 0)
    room:setPlayerMark(player, "@hx__neifa__trick", 0)
    room:setPlayerMark(player, "@hx__neifa__equip", 0)
  end,
})

hx__neifa:addEffect("prohibit", {
  name = "#hx__neifa_prohibit",
  prohibit_use = function(self, player, card)
    if player:getMark("@hx__neifa__basic") ~= 0 and card.type == Card.TypeBasic then return true end
    if player:getMark("@hx__neifa__trick") ~= 0 and card.type == Card.TypeTrick then return true end
    if player:getMark("@hx__neifa__equip") ~= 0 and card.type == Card.TypeEquip then return true end
  end,
})

hx__neifa:addEffect(fk.AfterCardTargetDeclared, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if (player:getMark("@hx__neifa__basic") > 0 and player:getMark("@hx__neifa__trick") > 0)
    or (player:getMark("@hx__neifa__basic") > 0 and player:getMark("@hx__neifa__equip") > 0) 
    or (player:getMark("@hx__neifa__trick") > 0 and player:getMark("@hx__neifa__equip") > 0) 
    or (player:getMark("@hx__neifa__draw") >= 4) then
      if target == player then
        if data.card:isCommonTrick() or data.card.type == Card.TypeBasic then
          local targets = player.room:getAlivePlayers()
          if #data.tos > 1 then
            table.insertTable(targets, data.tos)
          end
          if #targets > 0 then
            event:setCostData(self, {tos = targets})
            return true
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 2,
      targets = event:getCostData(self).tos,
      skill_name = hx__neifa.name,
      prompt = "#hx__neifa_trigger-choose:::"..data.card:toLogString(),
      cancelable = true,
      extra_data = table.map(data.tos, Util.IdMapper),
      target_tip_name = "addandcanceltarget_tip",
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    if table.contains(data.tos, to) then
      data:removeTarget(to)
      room:sendLog{
        type = "#RemoveTargetsBySkill",
        from = player.id,
        to = {to.id},
        arg = hx__neifa.name,
        arg2 = data.card:toLogString(),
      }
    else
      data:addTarget(to)
      room:sendLog{
        type = "#AddTargetsBySkill",
        from = player.id,
        to = {to.id},
        arg = hx__neifa.name,
        arg2 = data.card:toLogString(),
      }
    end
  end,
})

return hx__neifa